package org.example.biz.impl;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.bean.EmpInfo;
import org.example.biz.EmpBiz;
import org.example.mapper.EmpMapper;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class EmpBizImpl implements EmpBiz {
    private EmpMapper em;
    private SqlSession session;
    public EmpBizImpl(){
        InputStream in = EmpBizImpl.class.getClassLoader()
                .getResourceAsStream("config.xml");
        //获取会话工厂
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        //获取会话
        session = ssf.openSession();
        //获取EmpMapper的对象
        em = session.getMapper(EmpMapper.class);
    }

    @Override
    public List<EmpInfo> findAllEmp() {
        return em.findAllEmp();
    }

    @Override
    public EmpInfo findEmpByNo(int empNo) {
        return em.findEmpByNo(empNo);
    }

    @Override
    public List<EmpInfo> findEmpBySalAndJob1(Map<String, Object> map) {
        return findEmpBySalAndJob1(map);
    }

    @Override
    public List<EmpInfo> findEmpBySalAndJob2(EmpInfo ei) {
        return findEmpBySalAndJob2(ei);
    }

    @Override
    public List<EmpInfo> findEmpByPage(Map<String, Integer> map) {
        return findEmpByPage(map);
    }

    @Override
    public void saveEmp(EmpInfo ei) {
        em.saveEmp(ei);
        session.commit();
    }

    @Override
    public void deleteEmp(int empNo) {
        em.deleteEmp(empNo);
        session.commit();
    }

    @Override
    public void updateEmp(EmpInfo ei) {
        em.updateEmp(ei);
        session.commit();
    }
}
